package oracletojbpmwf.wizards;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ConfigureOracleToJbpmWFRuntimePage extends WizardPage {
	
	private static final String INPUT_WFT_MSG = "Choose the Oracle WFT file for conversion";
	private static final String INPUT_WFT_LOCATION_MSG = "Enter or search a location for the Oracle WFT file";
	private static final String CREATE_PROJECT_MSG = "Press next to create the project";
	private static final String UNEXISTING_WFT_MSG = "This file does not exist";
	@SuppressWarnings("unused")
	private static final String INVALID_WFT_FILE_MSG = "This file is not a valid Oracle WFT file. Please re-try";
	
	Text locationText;
	
	public ConfigureOracleToJbpmWFRuntimePage() {
		super("Choose workflow wft file to convert");
		setTitle("Choose workflow wft file to convert");
		setMessage(INPUT_WFT_MSG);
	}

	public void createControl(Composite parent) {
		Composite composite = createClientArea(parent);
		createLocateWFTFileGroup(composite);
		createSpace(composite);
		setControl(composite);
	}
	
	private void update() {
		updateMessage();
		updatePages();
	}
	
	private void updatePages() {
		setPageComplete(!isLocationEmpty());
		if (!isLocationEmpty()) {
			@SuppressWarnings("unused")
			OracleToJbpmWFWizardPage page = (OracleToJbpmWFWizardPage)getNextPage();
		}
	}
	
	private void createSpace(Composite composite) {
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(GridData.FILL_BOTH));
	}
	
	private void createLocateWFTFileGroup(Composite composite) {
		Group group = new Group(composite, SWT.NONE);
		group.setText("Select the oracle workflow file to convert");
		group.setLayout(new GridLayout(3, false));
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.verticalIndent = 5;
		group.setLayoutData(gridData);
		createJbpmRuntimeLocationField(group);
	}
	
	private void createJbpmRuntimeLocationField(Composite composite) {
		Label locationLabel = new Label(composite, SWT.NONE);
		locationLabel.setText("WFT file :");
		locationText = new Text(composite, SWT.BORDER);
		locationText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		locationText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				update();
			}
		});
		Button locationButton = new Button(composite, SWT.PUSH);
		locationButton.setText("Browse...");
		locationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				searchLocation();				
			}
		});
	}
	
	private void searchLocation() {
		FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
		dialog.setFilterExtensions(new String[]{"*.wft"});
		String result = dialog.open();
		if (result != null) {
			locationText.setText(result);
		}		
	}
	
	private Composite createClientArea(Composite parent) {
		Composite composite= new Composite(parent, SWT.NONE);
		GridLayout layout= new GridLayout();
		layout.marginWidth= 0;
		layout.marginHeight= 0;
		layout.numColumns= 1;
		composite.setLayout(layout);
		return composite;
	}
	
	public boolean updateMessage() {
		setErrorMessage(null);
		if (isLocationEmpty()) {
			setMessage(INPUT_WFT_LOCATION_MSG);
			return false;
		} else if (!isLocationExisting()) {
			setErrorMessage(UNEXISTING_WFT_MSG);
			return false;
		}else {
			setMessage(CREATE_PROJECT_MSG);
			return true;
		}
	}
	
	private boolean isLocationEmpty() {
		String text = locationText.getText();
		return text == null || "".equals(text);
	}
	
	private boolean isLocationExisting() {
		return new Path(locationText.getText()).toFile().exists();
	}
}
